#!/usr/bin/env python

import numpy as np
import pylab as pl
import os.path as osp

def extract_info(filep):
	qdict = {}
	line = '1'
	# info = np.array([False])
	while line:
		line = filep.readline()
		if line.startswith('GOP = '):
			tmp = line.replace('GOP = ','').replace('Qscale = ','').replace(':','').replace('\t',' ').replace('\n','')
			gop, qscale = tmp.split()
			gop = int(gop)
			qscale = int(qscale)
			fsize = filep.readline()
			fsize = int(fsize)
		if gop == 0:
			continue
		if not qdict.has_key(qscale):
			qdict[qscale] = np.array([gop, fsize])
		else: qdict[qscale] = np.vstack((qdict[qscale], [gop, fsize]))
	return qdict


def plot(qd):
	keys = qd.keys() 
	keys.sort() # Sort keys
	for key in keys:
		data_array = qd[key]
		pl.plot(data_array[:,0],data_array[:,1]/(1024*1024), label='Quantization = '+str(key), )
		print data_array[:,0],data_array[:,1]
	
	pl.xlim(1,128)
	pl.ylim(0,500)
	pl.legend()
	pl.xlabel('GOP size [-]')
	pl.ylabel('Filesize [MB]')

	pl.grid(True)
	
	

if __name__ == '__main__':

	filename = '/home/jeppe/Dropbox/P6/Diverse/gopstats_filesize.dat'
	f = open(filename,'r')

	info_array = extract_info(f)

	plot(info_array)

	svndir = osp.abspath('../../rapport/figs')

	pl.savefig(svndir+'/gopstats.eps')



		